package io.plus.user_interface.server.command.command_handler;

import io.plus.interactive.AsynchronousSocketChannelContext;
import io.plus.user_interface.common.SessionContext;
import io.plus.user_interface.common.Status;
import io.plus.user_interface.common.io;
import io.plus.user_interface.common.packet.*;
import io.plus.user_interface.common.protocol.PacketImpl;
import io.plus.user_interface.common.utils.ImKit;
import io.plus.user_interface.common.utils.JsonKit;
import io.plus.user_interface.server.command.AbstractCommandHandler;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

//import drds.io.common.packet.*;
//import user_interface.io.common.packet.*;


/**
 * 加入群组消息cmd命令处理器
 */
@Slf4j
@Data
public class JoinGroupCommandHandler extends AbstractCommandHandler {

    /**
     * 绑定群组
     */
    public PacketImpl bindGroup(PacketImpl packet, AsynchronousSocketChannelContext asynchronousSocketChannelContext) throws Exception {
        if (packet.getBody() == null) {
            throw new IllegalArgumentException("body is null");
        }
        Group group = JsonKit.toBean(packet.getBody(), Group.class);
        String groupId = group.getId();


        io.bindGroup(asynchronousSocketChannelContext, groupId, userConfiguration.getMessageHelper().getBindListener());


        JoinGroupResponseMessage joinGroupResponseMessage = new JoinGroupResponseMessage();
        joinGroupResponseMessage.setGroupId(groupId);
        JoinGroupResult joinGroupResult = JoinGroupResult.allow_join;
        joinGroupResponseMessage.setJoinGroupResult(joinGroupResult);

        ResponseMessage responseMessage = new ResponseMessage(Command.join_group_response, Status.C10011).setData(joinGroupResponseMessage);
        PacketImpl respPacket = ImKit.convertResponsepPacket(responseMessage, asynchronousSocketChannelContext);
        return respPacket;
    }

    @Override
    public Command command() {

        return Command.join_group_request;
    }

    @Override
    public PacketImpl handler(PacketImpl packet, AsynchronousSocketChannelContext asynchronousSocketChannelContext) throws Exception {
        //绑定群组;
        PacketImpl joinGroupRespPacket = bindGroup(packet, asynchronousSocketChannelContext);
        //发送进房间通知;
        joinGroupNotify(packet, asynchronousSocketChannelContext);
        return joinGroupRespPacket;
    }

    /**
     * 发送进房间通知;
     *
     * @param packet
     * @param asynchronousSocketChannelContext
     */
    public void joinGroupNotify(PacketImpl packet, AsynchronousSocketChannelContext asynchronousSocketChannelContext) {
        SessionContext sessionContext = (SessionContext) asynchronousSocketChannelContext.getAttribute();

        User clientUser = sessionContext.getClient().getUser();
        User notifyUser = new User(clientUser.getId(), clientUser.getNick());

        Group joinGroup = JsonKit.toBean(packet.getBody(), Group.class);
        String groupId = joinGroup.getId();
        //发进房间通知  COMMAND_JOIN_GROUP_NOTIFY_RESP
        JoinGroupNotifyRequestMessage joinGroupNotifyRequestMessage = new JoinGroupNotifyRequestMessage().setGroup(groupId).setUser(notifyUser);
        ResponseMessage notifyRespBody = new ResponseMessage(Command.join_group_notify_response, joinGroupNotifyRequestMessage);

        PacketImpl joinGroupNotifyRespPacket = new PacketImpl(Command.join_group_notify_response, notifyRespBody.toByte());
        io.sendToGroup(groupId, joinGroupNotifyRespPacket);
    }
}
